﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using RegionInterface;
using Common;


public class AvatarMovement
{
    public AvatarHandle Handle;
    public AvatarController Controller;
    public bool InitiativeJump = false;
    private const float WalkSpeed = 6;
    private Transform transform;
    private Vector3 targetPosition = Vector3.zero;
    private Vector3 forward = Vector3.zero;
    private Vector3 moveInput = Vector3.zero;
    private Vector3 mouseInput = Vector3.zero;

    private Vector3 leftAngle = new Vector3(0, 180, 0);
    private Vector3 lastPosition = Vector3.zero;
    private Vector3 rightAngle = Vector3.zero;
    private MovementType direction = MovementType.Right;
    
    public bool[] LastState = new bool[8];
    public MovementType Direction
    {
        set
        {
            if (direction != value && value != MovementType.None)
            {
                direction = value;
                if (direction == MovementType.Right) Handle.Root.transform.eulerAngles = rightAngle;
                else Handle.Root.transform.eulerAngles = leftAngle;
            }
        }
        get
        {
            return direction;
        }
    }
    public AvatarMovement(AvatarHandle handle)
    {
        Handle = handle;
        transform = Handle.Root.transform;
        Controller = new AvatarController(Handle);
        direction = MovementType.Right;
    }
    public void UpdatePosition(float y, float z)
    {
        if (Handle.RemoteInit)
        {
            Handle.InputHandle.MoveLeft = LastState[0];
            Handle.InputHandle.MoveRight = LastState[1];
            Handle.InputHandle.Jump = LastState[2];
            Handle.InputHandle.Run = LastState[3];
            transform.position = new Vector3(transform.position.x, y, z);
        }
    }
    public void FixedUpdate()
    {

    }
    private void movement()
    {
        Direction = Handle.InputHandle.MoveDirection;
        if (Handle.InputHandle.MoveDirection == MovementType.None)
        {
            mouseInput.z = 0;
            Handle.Animation.StartIdle();
        }
        else
        {
            mouseInput.z = WalkSpeed;
            Handle.Animation.StartWalk();
        }
        forward = transform.TransformDirection(mouseInput);
        if (Handle.InputHandle.Run)
        {
            forward.z = forward.z * 2.5f;
            Handle.Animation.StartRun();
        }
        if (Handle.InputHandle.Jump)
        {
            forward.y = 30;
            InitiativeJump = true;
            LastState[2] = Handle.InputHandle.Jump;
            Handle.Animation.StartJump();
        }
        else
        {
            forward.y -= 9;
        }

    }
    private float difZ = 0;
    private float difY = 0;
    public void Update()
    {
        //if (!Handle.IsOwner)//set scenes players alpha
        //{
        //    if (Mathf.Abs(Handle.Module.Owner.Root.transform.position.z - transform.position.z) > 1) Handle.Clothes.SetAlpha = false;
        //    else Handle.Clothes.SetAlpha = true;
        //}
        lastPosition = transform.position;
        if (Handle.InputHandle.ActionIdle)
        {
            if (InitiativeJump)
            {
                Direction = Handle.InputHandle.MoveDirection;
                if (Handle.InputHandle.MoveDirection != MovementType.None)
                {
                    //if (Handle.InputHandle.Run) Handle.InputHandle.Run = false;
                    mouseInput.z = WalkSpeed;
                    forward.z = transform.TransformDirection(mouseInput).z;
                    if (Handle.InputHandle.Run)
                    {
                        forward.z = forward.z * 2.5f;
                    }
                }
            }
            else movement();
        }
        else forward.z = 0;
        Handle.InputHandle.Jump = false;

        forward.y -= 60 * InputManager.DeltaTime;
        Controller.Move(forward * InputManager.DeltaTime);
        difZ = Math.Abs(transform.position.z - lastPosition.z);
        difY = Math.Abs(transform.position.y - lastPosition.y);
        if (difZ > .001f || difY > .001f) Handle.PositionChange(Handle as IUnit, transform.position);

        if (Handle.InputHandle.ActionIdle && !InputManager.OwnerInput.Grounded)
        {
            if (difZ < .001f) forward.z = 0;
            if (difY < .001f) forward.y = 0;
        } 

        if (Handle.IsOwner)
        {
            if (Handle.InputHandle.MoveLeft != LastState[0] || Handle.InputHandle.MoveRight != LastState[1] || Handle.InputHandle.Jump != LastState[2] || Handle.InputHandle.Run != LastState[3])
            {
                LastState[0] = Handle.InputHandle.MoveLeft;
                LastState[1] = Handle.InputHandle.MoveRight;
                LastState[3] = Handle.InputHandle.Run;
                Handle.Module.BroadcastPosition(transform.position.y, transform.position.z, DataHelp.SetOpetion(LastState));
                LastState[2] = Handle.InputHandle.Jump;
            }
        }
    }
}